<!--
 * @Author: your name
 * @Date: 2020-12-28 17:53:01
 * @LastEditTime: 2020-12-29 14:27:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \basicvue\vuehtml\vueindex.html
-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../assets/css/common.css">
    <link rel="stylesheet" href="../assets/css/vue.css">
    <script src="../assets/js/vue.js"></script>
</head>
<body>
<div id="app">
    <a href="../index.html" class="retu">BACK</a>
    <h1>Watch选项监控数据</h1>
    <span>{{ wenduRes }}</span>--
    <span>{{chuanyi}}</span>
    <hr>
    <button @click="addWendu(3)">增加温度</button>
    <button @click="reduceWendu(3)">减少温度</button>
</div>
<script>
    var app = new Vue({
        el : '#app',
        data : {
            wendu : 10,
            chuanyi : '建议穿皮夹克'
        },
        methods:{
            addWendu : function (num='') {
                if(num){
                    this.wendu += num;
                }else{
                    this.wendu ++;
                }
                return this.wendu+'℃';
            },
            reduceWendu : function (num='') {
                if(num){
                    this.wendu -= num;
                }else{
                    this.wendu --;
                }
            }
        },
        computed:{
            wenduRes:function () {
                return this.wendu+'℃';
            }
        },
        watch:{
            // wendu:function (newVal,oldVal) {
            //     if(newVal > 26){
            //         this.chuanyi = '建议穿T血短袖';
            //     }else if(newVal < 26 && newVal > 0){
            //         this.chuanyi = '建议夹克长裙';
            //     }else{
            //         this.chuanyi = '建议穿羽绒服';
            //     }
            // }
        }
    });

    //使用实例属性写watch监控
    var chuanyiArr = ['建议穿T血短袖','建议夹克长裙','建议穿羽绒服'];
    app.$watch('wendu',function (newVal,oldVal) {
        if(newVal > 26){
            this.chuanyi = chuanyiArr[0];
        }else if(newVal < 26 && newVal > 0){
            this.chuanyi = chuanyiArr[1];
        }else{
            this.chuanyi = chuanyiArr[2];
        }
    });


</script>
</body>
</html>